package com.ikingtech.platform.business.component.picker;

import com.ikingtech.framework.sdk.core.response.R;
import com.ikingtech.framework.sdk.utils.Tools;
import com.ikingtech.framework.sdk.web.annotation.ApiController;
import com.ikingtech.framework.sdk.web.annotation.PostRequest;
import com.ikingtech.framework.sdk.component.api.*;
import com.ikingtech.framework.sdk.component.model.*;
import io.swagger.v3.oas.annotations.Parameter;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.ArrayList;
import java.util.List;

/**
 * @author tie yan
 */
@RequiredArgsConstructor
@ApiController(value = "/component/pick", name = "业务组件", description = "业务组件")
public class PickerHandler {

    private final CompUserApi compUserApi;

    private final CompOrganizationApi compOrganizationApi;

    private final CompDepartmentApi compDepartmentApi;

    private final CompRoleApi compRoleApi;

    private final CompPostApi compPostApi;

    @PostRequest(order = 1, value = "/organization", summary = "组织选择组件", description = "组织选择组件。")
    public R<PickResultDTO> organizationPick(@Parameter(name = "queryParam", description = "查询条件")
                                             @RequestBody PickerElementQueryParamDTO queryParam) {
        PickResultDTO result = new PickResultDTO();
        result.setOrganizations(this.pickOrganization(queryParam));
        return R.ok(result);
    }

    @PostRequest(order = 1, value = "/department", summary = "部门选择组件", description = "部门选择组件。")
    public R<PickResultDTO> departmentPick(@Parameter(name = "queryParam", description = "查询条件")
                                           @RequestBody PickerElementQueryParamDTO queryParam) {
        PickResultDTO result = new PickResultDTO();
        ComponentOrganization rootOrg = this.compOrganizationApi.getRootOrganization();
        result.setRootOrganization(rootOrg);
        result.setRootDepartment(this.compDepartmentApi.getRootDepartment());
        if (Tools.Str.isNotBlank(queryParam.getParentOrgId())) {
            result.setOrganizations(this.pickOrganization(queryParam));
            result.setDepartments(this.pickDepartment(queryParam, rootOrg.getElementId()));
        }
        if (Tools.Str.isNotBlank(queryParam.getParentDepartmentId())) {
            result.setDepartments(this.pickDepartment(queryParam, rootOrg.getElementId()));
        }
        return R.ok(result);
    }

    @PostRequest(order = 2, value = "/role", summary = "角色选择组件", description = "角色选择组件。")
    public R<PickResultDTO> rolePick(@Parameter(name = "queryParam", description = "查询条件")
                                     @RequestBody PickerElementQueryParamDTO queryParam) {
        PickResultDTO result = new PickResultDTO();
        result.setRoles(this.pickRole(queryParam));
        return R.ok(result);
    }

    @PostRequest(order = 3, value = "/post", summary = "岗位选择组件", description = "岗位选择组件。")
    public R<PickResultDTO> postPick(@Parameter(name = "queryParam", description = "查询条件")
                                     @RequestBody PickerElementQueryParamDTO queryParam) {
        PickResultDTO result = new PickResultDTO();
        result.setPosts(this.pickPost(queryParam));
        return R.ok(result);
    }

    @PostRequest(order = 3, value = "/specified-user", summary = "用户(指定范围)选择组件", description = "用户(指定范围)选择组件。")
    public R<PickResultDTO> specifiedUserPick(@Parameter(name = "queryParam", description = "查询条件")
                                              @RequestBody PickerElementQueryParamDTO queryParam) {
        PickResultDTO result = new PickResultDTO();
        result.setUsers(this.pickUser(queryParam));
        return R.ok(result);
    }

    @PostRequest(order = 4, value = "/mix", summary = "混合选择组件", description = "混合选择组件。")
    public R<PickResultDTO> mixPick(@Parameter(name = "queryParam", description = "查询条件")
                                    @RequestBody PickerElementQueryParamDTO queryParam) {
        PickResultDTO result = new PickResultDTO();
        ComponentOrganization rootOrg = this.compOrganizationApi.getRootOrganization();
        result.setRootOrganization(rootOrg);
        result.setRootDepartment(this.compDepartmentApi.getRootDepartment());
        result.setOrganizations(this.pickOrganization(queryParam));
        result.setDepartments(this.pickDepartment(queryParam, rootOrg.getElementId()));
        result.setUsers(this.pickUser(queryParam));
        result.setRoles(this.pickRole(queryParam));
        result.setPosts(this.pickPost(queryParam));
        return R.ok(result);
    }

    private List<ComponentOrganization> pickOrganization(PickerElementQueryParamDTO queryParam) {
        if (Tools.Str.isNotBlank(queryParam.getName())) {
            return this.compOrganizationApi.listByName(queryParam.getName());
        }
        if (Tools.Str.isNotBlank(queryParam.getParentOrgId())) {
            return this.compOrganizationApi.listByParentId(queryParam.getParentOrgId());
        }
        return new ArrayList<>();
    }

    private List<ComponentDepartment> pickDepartment(PickerElementQueryParamDTO queryParam, String rootOrgId) {
        if (Tools.Str.isNotBlank(queryParam.getName())) {
            return this.compDepartmentApi.listByName(queryParam.getName());
        } else {
            if (Tools.Str.isNotBlank(queryParam.getParentOrgId()) &&
                    !"root".equals(queryParam.getParentOrgId())) {
                return this.compDepartmentApi.listByOrgId(queryParam.getParentOrgId());
            } else {
                if (Tools.Str.isBlank(queryParam.getParentDepartmentId())) {
                    return this.compDepartmentApi.listByOrgId(rootOrgId);
                } else {
                    return this.compDepartmentApi.listByParentId(queryParam.getParentDepartmentId());
                }
            }
        }
    }

    private List<ComponentRole> pickRole(PickerElementQueryParamDTO queryParam) {
        return this.compRoleApi.listByName(queryParam.getName());
    }

    private List<ComponentPost> pickPost(PickerElementQueryParamDTO queryParam) {
        return this.compPostApi.listByName(queryParam.getName());
    }

    private List<ComponentUser> pickUser(PickerElementQueryParamDTO queryParam) {
        if (Tools.Coll.isNotBlank(queryParam.getUserIds()) || Tools.Str.isNotBlank(queryParam.getName())) {
            return this.compUserApi.listByIdsAndName(queryParam.getUserIds(), queryParam.getName());
        } else {
            return this.compUserApi.listByDeptId(queryParam.getParentDepartmentId());
        }
    }
}
